const getOpname = /(query|mutation) ?([\w\d-_]+)? ?\(.*?\)? \{/

export default function nanographql(str) {
  str = Array.isArray(str) ? str.join('') : str
  const name = getOpname.exec(str)
  return function(variables) {
    const data = { query: str }
    if (variables) data.variables = JSON.stringify(variables)
    if (name && name.length) {
      const operationName = name[2]
      if (operationName) data.operationName = name[2]
    }
    return JSON.stringify(data)
  }
}
